package cn.pengpeng;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLReducerOutputValue extends MySQLOutputWritable {

    private String url;

    private String date;

    private int uv;

    @Override
    public String fetchInsertOrUpdateSql() {
        // 示例：插入语句
        return "insert into stats_uv(url, date, uv) values(?, ?, ?)";
    }

    @Override
    public void setPreparedStatementParameters(PreparedStatement pstmt) throws SQLException {
        pstmt.setString(1, this.url);
        pstmt.setString(2, this.date);
        pstmt.setInt(3, this.uv);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        if(this.url == null)
            out.writeBoolean(false);
        else {
            out.writeBoolean(true);
            out.writeUTF(this.url);
        }
        if(this.date == null)
            out.writeBoolean(false);
        else {
            out.writeBoolean(true);
            out.writeUTF(this.date);
        }
        out.writeInt(uv);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.url = in.readBoolean() ? in.readUTF() : null;
        this.date = in.readBoolean() ? in.readUTF() : null;
        this.uv = in.readInt();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getUv() {
        return uv;
    }

    public void setUv(int uv) {
        this.uv = uv;
    }
}